//
//  registeredViewController.m
//  HeSiad
//
//  Created by ZC on 15/4/25.
//  Copyright (c) 2015年 MWJ. All rights reserved.
//

#import "registeredViewController.h"
#import "AppDelegate.h"
#import "leftViewController.h"

#define registeredURL @"http://192.168.1.110:8888/hesaid/index.php/Hesaid/add_registered"
#define loginURL @"http://192.168.1.110:8888/hesaid/index.php/Hesaid/seek_login"

@interface registeredViewController (){
    NSString *str1;
    NSString *registerstring;
    UIAlertView *_alert;
    NSMutableArray *loginArray;
    NSString *loginstr;
    AppDelegate *app;
    NSUserDefaults *defaults;

}

@end

@implementation registeredViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"注册";
    app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    
    loginArray = [NSMutableArray array];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - 注册时候解析数据
-(void)registeredData{
    if ([self.phone.text isEqualToString:@""] || [self.validation.text isEqualToString:@""]) {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"手机号或验证码不能为空" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }else{
    NSURL *url=[NSURL URLWithString:registeredURL];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    str1 =[NSString stringWithFormat:@"user_phone=%@&user_validation=%@",self.phone.text,self.validation.text];
    NSData *data=[str1 dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        registerstring = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        [self performSelectorOnMainThread:@selector(refreshData) withObject:registerstring waitUntilDone:YES];
        int intStr = [registerstring intValue];
        
        if (intStr == 1) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"注册成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            _alert=alert;
            [alert show];
            //自动登录
            [self Login];
        }else{
            if (intStr == 0) {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"注册失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alert show];
            }else{
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"该手机号已被注册" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alert show];
            }
        }
    }];
    }
    
}

#pragma mark - 提示框的点击事件

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0&&alertView==_alert) {
            leftViewController *leftView=[[leftViewController alloc]initWithNibName:@"leftViewController" bundle:nil];
            [self.navigationController pushViewController:leftView animated:nil];
    }
}

#pragma mark - 登陆
-(void)Login{
    NSURL *url=[NSURL URLWithString:loginURL];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSLog(@"%@",self.phone.text);
    NSString *poststr =[NSString stringWithFormat:@"user_phone=%@",self.phone.text];
    NSData *data=[poststr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        loginArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshData) withObject:loginArray waitUntilDone:YES];
        for (NSDictionary *dic in loginArray) {
            NSLog(@"%@",dic);
            //注册之后保存用户id
            [app.defaults setObject:dic[@"user_id"] forKey:@"yonghu"];
            //同步
            [app.defaults synchronize];
        }
    }];
}

//-(void)SendVerificationCode{
//    NSURL *url = [NSURL URLWithString:Kyanzhengma];
//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//    [request setHTTPMethod:@"POST"];
//    str=[NSString stringWithFormat:@"phone=%@",self.phone.text];
//    NSLog(@"%@",self.phone.text);
//    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
//    [request setHTTPBody:data];
//    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//        sentstring = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//        [self performSelectorOnMainThread:@selector(refreshData) withObject:sentstring waitUntilDone:YES];
//        int intString = [sentstring intValue];
//        NSLog(@"%d",intString);
//        if (intString==0) {
//            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"验证码发送失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//            [alert show];
//        }else{
//            UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"提示" message:@"验证码已发送" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//            [alert1 show];
//        }
//    }];
//}

-(void)refreshData{
    
}

#pragma mark - 获取验证码

- (IBAction)getValidation:(id)sender {
    
    if ([self.phone.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入手机号码" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];

    }else{
        
    }
}

#pragma mark - 注册按钮

- (IBAction)registeredButton:(id)sender {
    
    [self registeredData];
    
}
@end
